package com.easyadmin.modules.job.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easyadmin.common.quartz.base.BaseQuartz;
import com.easyadmin.modules.job.domain.ScheduleLog;
import com.easyadmin.modules.job.mapper.ScheduleLogMapper;
import com.easyadmin.modules.stock.domain.SysBoundsEntity;
import com.easyadmin.modules.stock.domain.SysTradeDateEntity;
import com.easyadmin.modules.stock.mapper.SysTradeDateDao;
import com.easyadmin.modules.sys.mapper.SysLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;


@Slf4j
@Component("commonFuturesDate")
public class commonFuturesDate implements BaseQuartz {

    @Autowired
    private ScheduleLogMapper scheduleLogMapper;
    @Autowired
    SysTradeDateDao sysTradeDateDao;


    @Override
    public void run(String params) throws Exception {
        log.info(" 交易日期开始执行  commonFuturesDate ");
        LocalDate today = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 转换为字符串
        String todayStr = today.format(formatter);
        //判断当日数据是否执行
        List<ScheduleLog> data= scheduleLogMapper.selectCreateTime(todayStr,"commonFuturesDate");
        if(data.size()>0){
            return;
        }

        List<SysTradeDateEntity> DateLists =sysTradeDateDao.selectList(new QueryWrapper<SysTradeDateEntity>()
                .lambda().eq(SysTradeDateEntity::getSysDate,todayStr));
        if(DateLists.size()<1){
            //非交易日不执行
            return;
        }

        String currentPath = System.getProperty("user.dir");
        try {
            // 调用Python脚本futures_data.py
            Process p = Runtime.getRuntime().exec("python "+currentPath+"\\src\\main\\resources\\python\\futures_data.py");
            //SendMsg.SendDingDing("今日执行完成");
            // 获取脚本执行的结
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info(" 交易日期开始执行完成  commonFuturesDate ");
        //更新预警中的价格






















    }
}
